<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2022-04-14 14:26:46
 * @LastEditors: 小白
 * @LastEditTime: 2025-11-18 14:52:35
 * @FilePath: \tmsadmin\src\views\contract\template\components\list.vue
-->

<template>
  <SearchForm @search="searchFun" :houseList="houseList" style="margin-bottom: 20px" />
  <el-card class="page_card" shadow="never">
    <template #header>
      <div class="card-header">
        <div class="left">
          合同模板
            <el-button style="margin-left: 20px" type="primary" @click="add('新增')">
              新增模板
            </el-button>
            <!-- 按钮组 编辑 删除 重置密码 -->
            <el-button type="success" @click="edit('编辑')" style="margin-left: 20px">
              编辑模板
            </el-button>
            <el-button type="danger" @click="showDelete('删除')" style="margin-left: 20px">
              删除模板
            </el-button>
        </div>
        <div class="right">
          <el-button circle @click="refreshFun">
            <i class="icon ion-refresh" :class="iconRefresh" />
          </el-button>
        </div>
      </div>
    </template>
    <Table :table-data="tableData" :table-columns="columns" :page-size="pageSize" :page-num="pageNum" :loading="loading"
      :total="total" @edit="edit" @selectionChange="selectionChange" @change="changePage"
      @ok="getData()" />
    <Modal @ok="getData()" ref="modalRef"></Modal>
  </el-card>
</template>
<script lang="ts" setup>
import { REQUEST } from '@/api/request'
import { ElMessage, ElMessageBox } from 'element-plus'
import SearchForm from './search.vue'
import Table from './table.vue'
import Modal from './modal.vue'
import { onMounted, ref } from 'vue'
const queryForm = ref({})
const iconRefresh = ref('')

const pageNum = ref(1)
const pageSize = ref(10)
import { store } from '@/store'
const total = ref(0)
const modalRef = ref(null)
const loading = ref(false)
const tableData = ref([])
const columns = ref([
  {
    title: '模板名称',
    prop: 'templateName',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '模板内容',
    prop: 'template',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '创建时间',
    prop: 'createTime',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  }
])
onMounted(() => {
  getData()
})

const exportDocx = async (html: string) => {
  try {
    // 构建完整HTML结构
    const fullHtml = `
      <!DOCTYPE html>
      <html>
        <body style="font-family:方正仿宋_GBK,serif;mso-ascii-font-family:'Times New Roman'">
          ${html}
        </body>
      </html>
    `;

    // 转换为Word文档Blob
    const blob = await asBlob(fullHtml, {
      paperSize: 'A4',
      orientation: 'portrait',
      margins: { top: 100 } // 单位：pt
    });

    // 创建下载链接
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `文档_${new Date().getTime()}.docx`;
    
    // 触发下载
    document.body.appendChild(a);
    a.click();
    
    // 清理资源
    URL.revokeObjectURL(url);
    document.body.removeChild(a);
  } catch (error) {
    console.error('导出失败:', error);
  }
}
const houseList = ref([])
const add = (title: string) => {
  modalRef.value.openModal('新增模板')
}
const showDelete = (row: any) => {
  if (selectList.value.length > 0) {
    const ids = selectList.value.map((item: any) => item.id)
    ElMessageBox.confirm('您将进行删除操作，请确认', '提示', {
      distinguishCancelAndClose: true,
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(() => {
      REQUEST.POST('TEMPLATEDEL', {
        ids: ids
      }).then((res: any) => {
        if (res.status === 200) {
          ElMessage({
            type: 'success',
            message: '成功删除'
          })
          getData()
        } else {
          ElMessage({
            type: 'warning',
            message: res.message
          })
        }
      })
    })
  } else {
    ElMessage({
      type: 'warning',
      message: '请勾选数据！'
    })
  }
}
const getData = () => {
  // loading.value = true
  REQUEST.POST('TEMPLATELIST', { ...queryForm.value, 
     houseId: store.state.userInfo.houseId,
    pageNumber: pageNum.value, pageSize: pageSize.value }).then((res: any) => {
    tableData.value = res.data.records
    total.value = JSON.parse(res.data.total)
  })
}
const refreshFun = () => {
  iconRefresh.value = 'iconRefresh'
  getData()
  setTimeout(() => {
    iconRefresh.value = ''
  }, 1000)
}
const searchFun = (e: any) => {
  queryForm.value = e
  getData()
}
const selectList = ref([])
const selectionChange = (e: any) => {
  selectList.value = e
}

const edit = () => {
  if (selectList.value.length !== 1) {
    ElMessage.error('请选择一条数据进行编辑')
    return
  } else {
    modalRef.value.openModal('编辑合同模板', selectList.value[0])
  }
}
const changePage = (e: any) => {
  pageNum.value = e.pageNum
  pageSize.value = e.pageSize
  getData()
}
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
